package com.fernfx.xingtan.utils;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.os.Environment;
import android.provider.SyncStateContract;
import android.text.TextUtils;
import android.widget.EditText;

import com.fernfx.xingtan.common.base.BaseEntity;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 其他的一些操作工具类
 */
public final class OtherUtil {
    private OtherUtil() {
    }

    /*
     * editext 获得焦点
     */
    public static void editTextRequestFocus(EditText editText) {
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
    }

    /*
     * 验证是否成功
     */
    public static boolean checkRequestStatus(String isSucess, int code) {
        return "true".equalsIgnoreCase(isSucess) || 200 == code ? true : false;
    }

    /*
     * 验证是否成功
     */
    public static boolean checkRequestStatus(BaseEntity baseEntity) {
        return checkRequestStatus(baseEntity.getSuccess(), baseEntity.getStatus());
    }

    /*
     * 获取get请求url地址
     */
    public static String getGetUrl(String url, Map<String, Object> params) {
        // 添加url参数
        if (!CollectionUtil.isEmpty(params)) {
            Iterator<String> it = params.keySet().iterator();
            StringBuffer sb = null;
            while (it.hasNext()) {
                String key = it.next();
                String value = String.valueOf(params.get(key));
                if (sb == null) {
                    sb = new StringBuffer();
                    sb.append("?");
                } else {
                    sb.append("&");
                }
                sb.append(key);
                sb.append("=");
                sb.append(value);
            }
            url += sb.toString();
        }
        return url;
    }

    /*
     * 获取 class name
     */
    public static String getClassSimpleName(Class<?> cls) {
        return cls.getSimpleName().trim();
    }

    /*
     * 等于空加默认值
     */
    public static String checkEmptyDefault(String text) {
        return TextUtils.isEmpty(text) ? "" : text;
    }

    /*
 * 等于空加默认值
 */
    public static String checkEmptyDefault(String text, String defaultText) {
        return TextUtils.isEmpty(text) ? defaultText : text;
    }

    /*
     * 获得当前进程的名字
     *
     * @param context
     * @return
     */
    public static String getCurProcessName(Context context) {

        int pid = android.os.Process.myPid();

        ActivityManager activityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);

        for (ActivityManager.RunningAppProcessInfo appProcess : activityManager
                .getRunningAppProcesses()) {

            if (appProcess.pid == pid) {

                return appProcess.processName;
            }
        }
        return null;
    }

    /**
     * 是否有sd卡
     */
    public static boolean hasSdcard() {
        String state = Environment.getExternalStorageState();
        return state.equals(Environment.MEDIA_MOUNTED);
    }

    public static <T> T jsonConvertEntity(String json, Class<?> zClass) {
        return (T) FastJsonUtil.fromBean(json, zClass);
    }

    /**
     * 0 true 1 false
     */
    public static boolean getStatusFlag(int text) {
        if (text == 0){
            return true;
        }else {
            return false;
        }
    }
}
